<html>
	<head>
		<link rel="stylesheet" type="text/css" href="style.css">
		<?php
		function readFiles($dir){
			$handle=opendir ($dir);
			$filesarray = array();

			while ($datei = readdir ($handle)) {
				if(substr($datei,-4) == ".txt")
					$filesarray[] = $datei; //Nimmt erste freie Stelle
			}

			closedir($handle);
			return $filesarray;
		}
		
		function readSubFolder($dir){
			$handle = opendir($dir);
			$subfolder = array();
			
			while($folder = readdir($handle)) {
				if(!is_file($folder) && substr($folder,0,1) != ".")
					$subfolder[] = $folder;
			}
			
			closedir($handle);
			return $subfolder;
			
		}
		
		function parseString($string){
			$ret = str_replace("\n", "<br />", $string);
			$ret = str_replace("ü", "&uuml;", $ret);
			$ret = str_replace("ä", "&auml;", $ret);
			$ret = str_replace("ö", "&ouml;", $ret);
			$ret = str_replace("Ü", "&Uuml;", $ret);
			$ret = str_replace("Ä", "&Auml;", $ret);
			$ret = str_replace("Ö", "&Ouml;", $ret);
			$ret = str_replace("ß", "&szlig;", $ret);
			return $ret;
		}
		
		function getCookieArray() {
			if(!isset($_COOKIE['recentBooks']))
				return array();
			$cookie = $_COOKIE['recentBooks'];
			$cookie = stripslashes($cookie);
			$recentBooks = json_decode($cookie, true);
			return $recentBooks;
		}
		
		function saveCookieArray($recentBooks) {
			$json = json_encode($recentBooks, true);
			setcookie('recentBooks', $json);
		}
		
		function truncateLength($array, $length) {
			while(count($array) > $length)
				array_shift($array);
			return $array;
		}
			
			
			$letter = $_GET["letter"];
			if($letter == null) {
				?>
					<script type="text/javascript">
						window.location = "index.php";
					</script>
				<?php 
			}
			
			$file = $_GET["file"];
			if($file == null || !file_exists("./ebooks/$letter/$file")) {
				?>
					<script type="text/javascript">
						window.location = "index.php";
					</script>
				<?php 
			}
			
			$bookname = basename($file, ".txt");
			
		?>
		<script type="text/javascript">
			function getPageScroll() {
    			var xScroll, yScroll;
    			if (self.pageYOffset) {
      				yScroll = self.pageYOffset;
      				xScroll = self.pageXOffset;
			    } else if (document.documentElement && document.documentElement.scrollTop) {
			      	yScroll = document.documentElement.scrollTop;
			      	xScroll = document.documentElement.scrollLeft;
			    } else if (document.body) {// all other Explorers
			      	yScroll = document.body.scrollTop;
			      	xScroll = document.body.scrollLeft;
			    }
    			return new Array(xScroll,yScroll);
			}
			function setYScroll(yVal) {
				if (self.pageYOffset) {
      				self.pageYOffset = yVal;
			    } else if (document.documentElement && document.documentElement.scrollTop) {
			      	document.documentElement.scrollTop = yVal;
			    } else if (document.body) {// all other Explorers
			      	document.body.scrollTop = yVal;
			    }
			}
			function checkScroll(cookiename) {
				if(getCookie(cookiename) != "") {
					setYScroll(getCookie(cookiename));
				}
			}
			function setCookie(cname, cvalue, exdays) {
			    var d = new Date();
			    d.setTime(d.getTime() + (exdays*24*60*60*1000));
			    var expires = "expires="+d.toUTCString();
			    document.cookie = cname + "=" + cvalue + "; " + expires;
			}
			function getCookie(cname) {
    			var name = cname + "=";
    			var ca = document.cookie.split(';');
    			for(var i=0; i<ca.length; i++) {
        			var c = ca[i];
        			while (c.charAt(0)==' ') c = c.substring(1);
        			if (c.indexOf(name) != -1) return c.substring(name.length,c.length);
    			}
    			return "";
			} 
		</script>
		
	<title><?php echo $bookname; ?></title>
	<p>
	<?php 
		$recentBooks = getCookieArray();
		array_push($recentBooks, $letter . "/" . $bookname);
		$recentBooks = array_reverse($recentBooks);
		$recentBooks = array_unique($recentBooks);
		$recentBooks = array_reverse($recentBooks);
		$recentBooks = truncateLength($recentBooks, 15);
		saveCookieArray($recentBooks);
	?>
	</p>
	</head>
	<body onbeforeunload="setCookie(<?php echo "'" . $bookname . "scrollPos'" ?> ,getPageScroll()[1],1);" onload="checkScroll(<?php echo "'" . $bookname . "scrollPos'" ?>)">
		<script type="text/javascript" src="sidebarLetters.js"></script>
		<div id="title">
			EBook-Reader
		</div>
		<div id="bar">
			<div id="sidebarLetters-close">
				<a onclick="javaScript: openLetters()"> ||| </a>
			</div>
			<div id="letterlist" isVisible="false">
				<?php
					echo "<a class='letter' href='index.php'>&nbsp; @ &nbsp;</a>";
					for($i = 1; $i <= 26; $i++){
						echo "<a class='letter";
						if($letter == chr(64 + $i)) echo "Cur";
						echo "' href='makelist.php?letter=";
						echo chr(64 + $i);
						echo "'>&nbsp; ";
						echo chr(64 + $i);
						echo " &nbsp;</a>";
					}
				?>
			</div>
		</div>
		<div id="content">
			<?php	
				if(file_exists("./ebooks/$letter/$bookname.jpg"))
					echo "<img id='coverimg' src='./ebooks/$letter/$bookname.jpg' width='256' height='410' vspace='10' hspace='20' align='left'>";
				echo "<p class='bookcontent'>";
				$bookdata = explode("-", $bookname);
				echo "--------------------------------------------<br /><br />";
				echo strtoupper($bookdata[0]) . "<br /><br />";
				echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-<br /><br />"; 
				echo $bookdata[1] . "<br /><br />";
				echo "--------------------------------------------<br /><br />";
	 			echo parseString(file_get_contents("./ebooks/$letter/$file"));
				echo "</p>";
			?>
		</div>
		<div id="sidebar">
				
	  	<ul>
	  		<?php
	  			$split = explode("-", $bookname);
				$author = $split[0];
	  		?>
		    <li><a href="index.php">Startseite</a></li>
		    <li><a href="makelistAuthor.php?<?php echo "author=$author&letter=$letter"; ?>"><!--Zur&uuml;ck -->zum Autoren</a></li>
		    <li><a href="makelist.php?<?php echo "letter=$letter"; ?>"><!--Zur&uuml;ck -->zum Buchstaben</a></li>
		</ul>
	
		</div>
	</body>
</html>